import request from '@/utils/request'

// 分页查询课程
export function getCoursePage(params) {
  return request({
    url: '/v1/courses',
    method: 'get',
    params
  })
}

// 课程管理
export function getCourseList(params) {
  return request({
    url: '/v1/courses',
    method: 'get',
    params
  })
}

export function getCourseDetail(id) {
  return request({
    url: `/v1/courses/${id}`,
    method: 'get'
  })
}

export function createCourse(data) {
  return request({
    url: '/v1/courses',
    method: 'post',
    data
  })
}

export function updateCourse(id, data) {
  return request({
    url: `/v1/courses/${id}`,
    method: 'put',
    data
  })
}

export function deleteCourse(id) {
  return request({
    url: `/v1/courses/${id}`,
    method: 'delete'
  })
}

export function publishCourse(id) {
  return request({
    url: `/v1/courses/${id}/publish`,
    method: 'post'
  })
}

export function unpublishCourse(id) {
  return request({
    url: `/v1/courses/${id}/unpublish`,
    method: 'post'
  })
}

// 章节管理
export function getChapterList(courseId) {
  return request({
    url: `/v1/chapters/course/${courseId}`,
    method: 'get'
  })
}

export function createChapter(data) {
  return request({
    url: '/v1/chapters',
    method: 'post',
    data
  })
}

export function updateChapter(id, data) {
  return request({
    url: `/v1/chapters/${id}`,
    method: 'put',
    data
  })
}

export function deleteChapter(id) {
  return request({
    url: `/v1/chapters/${id}`,
    method: 'delete'
  })
}

export function getChapterDetail(id) {
  return request({
    url: `/v1/chapters/${id}`,
    method: 'get'
  })
}

export function updateChapterSort(courseId, chapters) {
  return request({
    url: `/v1/chapters/course/${courseId}/sort`,
    method: 'put',
    data: chapters
  })
}

export function getChapterPage(params) {
  return request({
    url: '/v1/chapters/page',
    method: 'get',
    params
  })
}

// 课时管理
export function getLessonList(chapterId) {
  return request({
    url: `/v1/lessons/chapter/${chapterId}`,
    method: 'get'
  })
}

export function createLesson(data) {
  return request({
    url: '/v1/lessons',
    method: 'post',
    data
  })
}

export function updateLesson(id, data) {
  return request({
    url: `/v1/lessons/${id}`,
    method: 'put',
    data
  })
}

export function deleteLesson(id) {
  return request({
    url: `/v1/lessons/${id}`,
    method: 'delete'
  })
}

export function getLessonDetail(id) {
  return request({
    url: `/v1/lessons/${id}`,
    method: 'get'
  })
}

export function updateLessonSort(chapterId, lessons) {
  return request({
    url: `/v1/lessons/chapter/${chapterId}/sort`,
    method: 'put',
    data: lessons
  })
}

export function getLessonPage(params) {
  return request({
    url: '/v1/lessons/page',
    method: 'get',
    params
  })
}

export function uploadLessonVideo(lessonId, file) {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: `/v1/lessons/${lessonId}/video`,
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

export function getLessonProgress(lessonId, userId) {
  return request({
    url: `/v1/lessons/${lessonId}/progress/${userId}`,
    method: 'get'
  })
}

// 课程统计
export function getCourseStatistics(courseId) {
  return request({
    url: `/v1/courses/${courseId}/statistics`,
    method: 'get'
  })
}

// 批量导入课程
export function batchImportCourses(data) {
  return request({
    url: '/v1/courses/batch-import',
    method: 'post',
    data
  })
}

// 导出课程
export function exportCourses(courseIds) {
  return request({
    url: '/v1/courses/export',
    method: 'post',
    data: courseIds,
    responseType: 'blob'
  })
}

// ==================== 课程详情相关 ====================

// 获取课程章节（别名）
export function getCourseChapters(courseId) {
  return getChapterList(courseId)
}

// 获取课程统计（别名）
export function getCourseStats(courseId) {
  return getCourseStatistics(courseId)
}

// 获取课程学员
export function getCourseStudents(courseId) {
  return request({
    url: `/v1/courses/${courseId}/students`,
    method: 'get'
  })
}

// 获取课程评价
export function getCourseReviews(courseId) {
  return request({
    url: `/v1/courses/${courseId}/reviews`,
    method: 'get'
  })
}

// 获取最近学员
export function getRecentStudents(courseId) {
  return request({
    url: `/v1/courses/${courseId}/recent-students`,
    method: 'get'
  })
}

// 复制课程
export function duplicateCourse(courseId) {
  return request({
    url: `/v1/courses/${courseId}/duplicate`,
    method: 'post'
  })
}